//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.PageFlow;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.Product.Interfaces.Services;
using WCSFContrib.ECommerceCatalog.User.Interfaces.Services;
using WCSFContrib.ECommerceCatalog.User.Services;
using WCSFContrib.ECommerceCatalog.Admin.Interfaces.Services;

namespace WCSFContrib.ECommerceCatalog.User
{
    public class UserController : IUserController
    {
        private IUsersProfilesService _usersProfilesService;
        private IOrdersService _ordersService;
        private ICartService _cartService;
        private IItemsService _itemsService;
        private INavigationService _navigationService;

        [InjectionConstructor]
        public UserController([ServiceDependency] IUsersProfilesService usersProfilesService,
                              [ServiceDependency] IOrdersService ordersService,
                              [ServiceDependency] ICartService cartService,
                              [ServiceDependency] IItemsService itemsService,
                              [ServiceDependency] INavigationService navigationService,
                              [ProviderDependency(typeof(PageFlowDirectory))] IPageFlowProvider pageFlowProvider)
        {
            _usersProfilesService = usersProfilesService;
            _ordersService = ordersService;
            _cartService = cartService;
            _itemsService = itemsService;
            _navigationService = navigationService;

            _navigationService.PageFlowProvider = pageFlowProvider;
        }

        public UserController()
        { }

        #region IUserController Members

        public string CurrentPage
        {
            get { return _navigationService.UserPageFlow.CurrentPage.Name; }
            set { _navigationService.UserPageFlow.SetCurrentPage(value); }
        }

        public void StartUserPageFlow()
        {
            if (_navigationService.UserPageFlow.Status == PageFlowStatus.NotStarted)
                _navigationService.UserPageFlow.Start();
        }

        public Cart GetCartByUserName(string userName)
        {
            Cart cart = _cartService.GetCartByUserName(userName);
            return cart;
        }

        public ICollection<Order> GetOrdersByUserName(string userName)
        {
            ICollection<Order> orders = _ordersService.GetOrdersByUserName(userName);
            return orders;
        }

        public Item GetProduct(int productId)
        {
            Item product = _itemsService.GetItemById(productId);
            return product;
        }

        public void RemoveItemFromCart(string userName, int productId, int quantity)
        {
            Item item = _itemsService.GetItemById(productId);
            _cartService.RemoveItem(userName, item, quantity);
        }

        public void OrderCheckout(string userName, DateTime orderDate, Address billingAddress, Address shippingAddress, Payment paymentMethod)
        {
            Cart cart = GetCartByUserName(userName);
            _ordersService.Checkout(userName, cart.CartItems, orderDate, billingAddress, shippingAddress, paymentMethod);
            _cartService.DeleteCart(userName);

            _navigationService.GoUserInfo();
            //_navigationService.UserPageFlow.Complete();
        }

        public void GoCheckoutPage()
        {
            _navigationService.GoCheckout();
        }

        public void GoUserProfilePage()
        {
            _navigationService.GoUserInfo();
        }

        public void GoCartPage()
        {
            _navigationService.GoCart();
        }

        public UserProfile GetUserProfile(string userName)
        {
            UserProfile userProfile = _usersProfilesService.GetUserProfileByUserName(userName);
            return userProfile;
        }

        public void AddAddress(int userProfileId, Address address)
        {
            _usersProfilesService.AddAddress(userProfileId, address);
        }

        public void AddPaymentMethod(int userProfileId, Payment payment)
        {
            _usersProfilesService.AddPaymentMethod(userProfileId, payment);
        }

        public void RemoveAddress(int userProfileId, int addressId)
        {
            _usersProfilesService.RemoveAddress(userProfileId, addressId);
        }

        public void RemovePaymentMethod(int userProfileId, int paymentId)
        {
            _usersProfilesService.RemovePaymentMethod(userProfileId, paymentId);
        }

        public void CreateUserProfile(string userName)
        {
            _usersProfilesService.CreateUserProfile(userName);
        }

        public void UpdateAddress(int userProfileId, Address address)
        {
            _usersProfilesService.UpdateAddress(userProfileId, address);
        }

        public void UpdatePayment(int userProfileId, Payment payment)
        {
            _usersProfilesService.UpdatePaymentMethod(userProfileId, payment);
        }

        public Address GetAddresById(int addressId)
        {
            Address address = _usersProfilesService.GetAddressById(addressId);
            return address;
        }

        public Payment GetPaymentById(int paymentId)
        {
            Payment payment = _usersProfilesService.GetPaymentById(paymentId);
            return payment;
        }

        public void ClearErrors()
        {
            _navigationService.GoPageFlowError();
        }

        #endregion
    }
}
